//app.js
var validate=require("./utils/validata.js");
var blu=require("./utils/blue.js");
var net = require("./utils/net.js");
App({
  validate:validate,
  onLaunch:function(options){
    this.init(options);    
  },
  onShow:function(){
    console.log("app.js onshow");
  },
  onHide:function(){
    console.log("app.js onhide");
  },
  onError:function(){
    console.log("app.js onError");
  },
  globalData: {
    userInfo: null,    
    current_con_deviceId:null,//当前连接设备id
    current_con_serviceId: ["0000FFF0-0000-1000-8000-00805F9B34FB","0000180D-0000-1000-8000-00805F9B34FB"],//当前使用的serviceId
    charset_write: ["0000FFF2-0000-1000-8000-00805F9B34FB"],//write 特征值
    charset_notify: ["0000FFF1-0000-1000-8000-00805F9B34FB"],//notify 特征值
    able_serviceIds: ["0000FFF0-0000-1000-8000-00805F9B34FB","0000180D-0000-1000-8000-00805F9B34FB"],  //可搜索的serviceId   ,
    blue_is_connectd:false,//蓝牙是否连接到设备
    blue_adapter_able:false,// 蓝牙是否打开
    blue_discovering: false,// 蓝牙是否处于搜索状态
    screenW:320,
    screenH:568,
    isIOS:false
  },
  //初始化
  init:function(options){
    var that = this;
    //获取屏幕大小
    wx.getSystemInfo({
      success: function (res) {        
        var windowWidth = res.screenWidth;
        var windowHeight = res.screenHeight;
        that.globalData.screenW = windowWidth
        that.globalData.screenH = windowHeight
        that.globalData.isIOS = res.platform == "ios" ? true : false;
      }
    }) ;
    //开启蓝牙适配器
    wx.openBluetoothAdapter({
      success: function (res) {
        getApp().globalData.blue_adapter_able = true;
      },
    })
    //上传运动数据 在刷新的时候上传
    // var userinfo=wx.getStorageSync("userinfo");
    // var data={
    //     "bracelet_id":userinfo.bracelet_id,
    //     "pace": wx.getStorageSync("bracelet_data.current_pace"),
    //     "mileage": wx.getStorageSync("bracelet_data.current_mileage"),
    //     "sport_carorie": wx.getStorageSync("bracelet_data.current_calorie"),
    //     "sync_data": wx.getStorageSync("bracelet_data.sync_data"),
    //     "max_heart":wx.getStorageSync("bracelet_data.max_heart"),
    //     "min_heart": wx.getStorageSync("bracelet_data.min_heart"),
    //     "sport_consuming": wx.getStorageSync("bracelet_data.sport_consuming"),
    //     "shop_id": userinfo.shop_id
    // }
    // net.upload_data(data);
    //开启蓝牙心跳检测
    setInterval(that.blue_listen,5000);
    // 跳转到对应的页面
    if (options.scene == 1305) {//公众号菜单
      wx.setStorageSync("shop_id", options.query.shop_id);
    } else {
      wx.setStorageSync("shop_id", 20);
    }
    var has_login = wx.getStorageSync("has_login");
    // var has_login = wx.setStorageSync("has_login",false);
    // if (has_login) {
    //   wx.redirectTo({
    //     url: '/pages/loginfast/loginfast',
    //   })
    // } else {
    //   wx.redirectTo({
    //     url: '/pages/login/login',
    //   })
    // }      

  }, 
    //蓝牙每5秒监听
  blue_listen:function(){
   // console.log("blue_listen----蓝牙自动连接监听");
    if (getApp().globalData.blue_adapter_able==true) {//判断蓝牙是否打开
      if (!getApp().globalData.blue_is_connectd){//当前没有链接
            var devices = wx.getStorageSync("device_list");//获取当前设备列表
            if((devices instanceof Array)&&devices.length>0){//数组不为空              
              var deviceId = devices[0].deviceId;
              if(deviceId!=null){
                blu.blue_con(deviceId);
              }              
            }                
          }
      }
    },
     
})